<?php
namespace Orm\TrackLog;

use Base\Exception\RmException;
class TrackLogClass extends \Base\BaseOrmObject{
    protected   $id,
				$sid = 0, // 来源id
				$pid = 0, // 页面id
				$resource = '', // 请求资源
				$timestamp = 0, // 穿透页面的缓存  参数
				$oid = '', // 订单id
				$cid = '', // 渠道id
				$uid = '', // 用户id
				$guid = '', // 唯一识别码
				$is_del = 0, // 删除状态 0-正常 1-删除
				$insert_time = '', // 插入时间
				$update_time = ''; // 更新时间

    function __init($data)
    {
        parent::__init($data);
        $this->dformat($data);
    }

    private function dformat()
    {
    }

    public function loadById($id = 0)
    {
        $model = new \Orm\TrackLog\TrackLogModel();
        $data = $model->loadById($id);
        self::__init($data);
    }

    public function insert()
    {
        $this->checkNecessaryParam();
        $data = array(
            'sid' => $this->sid,
			'pid' => $this->pid,
			'resource' => $this->resource,
			'timestamp' => $this->timestamp,
			'oid' => $this->oid,
			'cid' => $this->cid,
			'uid' => $this->uid,
			'guid' => $this->guid,
        );

        $model = new \Orm\TrackLog\TrackLogModel();
        $model->insert($data);
        $this->id = $model->getLastId();
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '新增', $this->backup_data);
        /******************************************************/
    }

    public function update()
    {
        $this->checkNecessaryParam();
        $data = array(
            'sid' => $this->sid,
			'pid' => $this->pid,
			'resource' => $this->resource,
			'timestamp' => $this->timestamp,
			'oid' => $this->oid,
			'cid' => $this->cid,
			'uid' => $this->uid,
			'guid' => $this->guid,
        );

        $model = new \Orm\TrackLog\TrackLogModel();
        $model->update($data, "`id` = {$this->id}");
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '更新', $this->backup_data);
        /******************************************************/
    }

    public function delete($id)
    {
        $data = array('is_del'=>1);
        $model = new \Orm\TrackLog\TrackLogModel();
        $model->update($data, "`id` = {$id}");
        /******************************************************/
        parent::setBackupData(array_keys($data));
        $model->log($this->id, '删除', $this->backup_data);
        /******************************************************/
    }

    private function checkNecessaryParam()
    {
    }
}